﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OakEngine.AI.State_Machine
{
    public class Oak_StateMachine <T>
    {
        // The Agent
        T m_Owner;

        // THe current STate
        Oak_IState<T> m_CurrentState;
        public Oak_IState<T> CurrentState { get { return m_CurrentState; } set { m_CurrentState = value; } }

        // THe previous State
        Oak_IState<T> m_PreviousState;
        public Oak_IState<T> PreviousState { get { return m_PreviousState; } set { m_PreviousState = value; } }

        //This state logic is called everytime the FSM is updated
        Oak_IState<T> m_GlobalState;
        public Oak_IState<T> GlobalState { get { return m_GlobalState; } set { m_GlobalState = value; } }

        public Oak_StateMachine(T owner)
        {
            m_Owner = owner;
            m_CurrentState = null;
            m_PreviousState = null;
            m_GlobalState = null;
        }

        public void Update()
        {
            if (m_GlobalState != null)
                m_GlobalState.Execute(m_Owner);

            if (m_CurrentState != null)
                m_CurrentState.Execute(m_Owner);
        }

        public void ChangeState(Oak_IState<T> state)
        {
            // Store the Previous State
            m_PreviousState = m_CurrentState;

            // Exit the Current State
            m_CurrentState.Exit(m_Owner);

            // CHange the new state
            m_CurrentState = state;

            // Lets enter that state
            m_CurrentState.Enter(m_Owner);
        }

        public void BackPreviousState()
        {
            ChangeState(m_PreviousState);
        }

    }
}
